/**
 * WechatServer.java
 *
 * @author: ZhuJiahui
 * @date: 2018/8/26 15:10
 */
package com.zhujiahui.pattern.design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 被观察者实现
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class WechatServer implements Observerable {

    private List<Observer> observerList;

    private String message;

    public WechatServer() {
        this.observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        this.observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if (!this.observerList.isEmpty()) {
            this.observerList.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        this.observerList.forEach(observer -> observer.update(this.message));
    }

    /**
     * 设置消息内容
     *
     * @param passingMessage 消息
     */
    public void setInformation(String passingMessage) {
        this.message = passingMessage;
        System.out.println("服务更新消息： " + passingMessage);
        // 通知观察者
        this.notifyObserver();
    }
}
